package servlet;
import Dao.StudentDao;
import Dao.jdbcTools;
import Model.Student;
import Model.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/logina")
public class UserRequest2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String user = req.getParameter("user");
        String pwd = req.getParameter("pwd");
        String code = req.getParameter("yz");
        System.out.println("user2 = " + user);
        System.out.println("pwd2 = " + pwd);
        System.out.println("code= " + code);
//判断用户输入的验证码跟机器生成的验证码是否相同
        HttpSession session = req.getSession();
        String yz = (String) session.getAttribute("yz");
        if (!yz.equals(code)){
            System.out.println("验证码错误！");
        }
        //访问Dao， 查询记录
        //String sql="select * from guan where username=? and password=?";
        InputStream  in = Resources.getResourceAsStream("config/mybatis-config.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        SqlSession  sqlSession = factory.openSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        User select = studentDao.login(user, pwd);
        //User select = jdbcTools.select(sql, User.class, user, pwd);
        if (select==null){
            //错误提示
            req.getRequestDispatcher("usererror").forward(req,resp);
        }else {
//只要成功登录，则立即产生每个页面共享的session值
            //因此每一个后端管理员是否拥有session共享值进行判断是否合法登录
            session.setAttribute("user","passok");
            List<Student> list = studentDao.findAll();
            //System.out.println("mmmm");
//                //获取数据库中展示数据
//                List<Student> list = jdbcTools.selectall("select * from stuhhh", Student.class);
//                //数据展示
//            req.setAttribute("list",list);
//            req.getRequestDispatcher("showlist").forward(req,resp);
          //  req.setAttribute("list",list);
           // req.getRequestDispatcher("showlist1.jsp").forward(req,resp);
            req.setAttribute("list",list);
            req.getRequestDispatcher("showlist4.jsp").forward(req,resp);

        }


        //System.out.println("all = " + all);
    }

}
